package com.xinqi.modules.chat.chat.friend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xinqi.common.core.constant.MQConstants;
import com.xinqi.common.core.enums.RocketMqEnum;
import com.xinqi.common.core.enums.RocketMqRongyunEnum;
import com.xinqi.common.core.enums.SysMessageEnum;
import com.xinqi.common.rocketmq.dto.ChatMessageMessageBody;
import com.xinqi.common.rocketmq.dto.MqMessageDTO;
import com.xinqi.common.rocketmq.event.ProducerEvent;
import com.xinqi.modules.chat.chat.friend.domain.ChatFriendApplyEntity;
import com.xinqi.modules.chat.chat.template.TemplateMessageDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Objects;

/**
 * @version V1.0
 * @Title: 私聊融云通知消息
 * @ClassName: com.xinqi.modules.chat.chat.friend.service.ChatFriendMessageService.java
 * @Description:
 * @Copyright 星齐科技（云南）有限公司 - Powered By 技术部
 * @author: yyzhen
 * @date: 2023/7/26 15:26
 */
@Log4j2
@Validated
@Service("chatFriendMessageService")
@RequiredArgsConstructor
public class ChatFriendMessageService {

    /**
     * 申请添加好友
     *
     * @param apply
     */
    @Async("threadPoolTaskExecutor")
    public void sendApplyMsg(ChatFriendApplyEntity apply) {
        if (Objects.isNull(apply)) {
            return;
        }
        TemplateMessageDTO templateMessage = new TemplateMessageDTO();
        templateMessage.setType(SysMessageEnum.APPLY_FRIEND.getCode());
        templateMessage.setFromUserId(apply.getApplyerId().toString()); // 0代表系统用户
        templateMessage.setToUserId(apply.getReceverId().toString());
        templateMessage.setContent("申请添加好友");

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_SYSTEM_MESSAGE.getCode());
        body.setSenderId(apply.getApplyerId().toString());
        body.setTargetIds(new String[]{apply.getApplyerId().toString(), apply.getReceverId().toString()});
        body.setJson(JSONObject.toJSONString(templateMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.SYSTEM_MESSAGE_KEY + apply.getApplyerId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 添加好友成功
     *
     * @param apply
     */
    @Async("threadPoolTaskExecutor")
    public void sendAddFriendMsg(ChatFriendApplyEntity apply) {
        if (Objects.isNull(apply)) {
            return;
        }
        TemplateMessageDTO templateMessage = new TemplateMessageDTO();
        templateMessage.setType(SysMessageEnum.ADD_FRIEND.getCode());
        templateMessage.setFromUserId(apply.getReceverId().toString()); // 0代表系统用户
        templateMessage.setToUserId(apply.getApplyerId().toString());
        templateMessage.setContent("添加好友成功");

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_SYSTEM_MESSAGE.getCode());
        body.setSenderId(apply.getReceverId().toString());
        body.setTargetIds(new String[]{apply.getApplyerId().toString(), apply.getReceverId().toString()});
        body.setJson(JSONObject.toJSONString(templateMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.SYSTEM_MESSAGE_KEY + apply.getReceverId());
        ProducerEvent.producerFifoMessage(message);
    }

    /**
     * 删除好友成功
     *
     * @param userId
     * @param friendId
     */
    @Async("threadPoolTaskExecutor")
    public void sendDeleteFriendMsg(Long userId, Long friendId) {
        if (Objects.isNull(userId)) {
            return;
        }
        if (Objects.isNull(friendId)) {
            return;
        }
        TemplateMessageDTO templateMessage = new TemplateMessageDTO();
        templateMessage.setType(SysMessageEnum.DELETE_FRIEND.getCode());
        templateMessage.setFromUserId(userId.toString()); // 0代表系统用户
        templateMessage.setToUserId(friendId.toString());
        templateMessage.setContent("删除好友成功");

        ChatMessageMessageBody body = new ChatMessageMessageBody();
        body.setType(RocketMqRongyunEnum.RONGYUN_SYSTEM_MESSAGE.getCode());
        body.setSenderId(userId.toString());
        body.setTargetIds(new String[]{userId.toString(), friendId.toString()});
        body.setJson(JSONObject.toJSONString(templateMessage));
        MqMessageDTO message = new MqMessageDTO(RocketMqEnum.RONGYUN_MESSAGE.getCode(), JSONObject.toJSONString(body));
        message.setMessageKey(MQConstants.SYSTEM_MESSAGE_KEY + userId);
        ProducerEvent.producerFifoMessage(message);
    }

}
